package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.result.School;
import com.youhome.service.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 房源报告
 */
@RestController
@RequestMapping(value = "/report")
@Api(tags = "房源报告")
public class ReportController {

    @Autowired
    private TruliaDataService truliaDataService;
    @Autowired
    private ListHubService listHubService;
    @Autowired
    private CityDataService cityDataService;
    @Autowired
    private HomeJunctionService homeJunctionService;
    @Autowired
    private ZillowService zillowService;
    @Autowired
    private ReportService reportService;

    /**
     * 房源介绍
     *
     * @param houseId
     * @return
     */
    @ApiOperation(value = "房源介绍", notes = "房源介绍", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/listings/{houseId}")
    public Object Listings(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectListingDescription(houseId));
    }

    @ApiOperation(value = "当前房源房价预测", notes = "当前房源房价预测", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/currenthomevalueforecast/{houseId}")
    public Object currentHomeValueForecast(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectHomeValueForecast(houseId));
    }

    @ApiOperation(value = "房价环比", notes = "房价环比", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "time", value = "时间（年）", required = true, paramType = "query"),
    })
    @GetMapping(value = "/mom/{zipcode}")
    public Object mom(@PathVariable("zipcode") String zipcode, @RequestParam(value = "time") String time) throws CustomException {
        return new Result(ResultCode.SUCCESS, zillowService.selectMOM(zipcode, time));
    }

    @ApiOperation(value = "房价同比增长率", notes = "房价同比增长率", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "time", value = "时间(年)", required = true, paramType = "query"),
    })
    @GetMapping(value = "/yoy/{zipcode}")
    public Object yoy(@PathVariable("zipcode") String zipcode, @RequestParam(value = "time") String time) throws CustomException {
        return new Result(ResultCode.SUCCESS, zillowService.selectYOY(zipcode, time));
    }


    @ApiOperation(value = "通勤指数", notes = "通勤指数", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/commuteindex/{zipcode}")
    public Object CommuteIndex(@PathVariable("zipcode") String zipcode) throws CustomException {
        return new Result(ResultCode.SUCCESS, truliaDataService.selectCommuteIndexByZip(zipcode));
    }

    @ApiOperation(value = "失业率", notes = "失业率", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/unemployed/{zipcode}")
    public Object unemployed(@PathVariable("zipcode") String zipcode) throws CustomException {
        return new Result(ResultCode.SUCCESS, cityDataService.selectUnemployedByZip(zipcode));
    }

    @ApiOperation(value = "房租中位数时间图", notes = "房租中位数时间图", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "time", value = "时间(单位年,0则返回所有数据)", required = true, paramType = "query")
    })
    @GetMapping(value = "/medianrent/{zipcode}")
    public Object MedianRent(@PathVariable("zipcode") String zipcode, @RequestParam(value = "time") String time) throws CustomException {
        return new Result(ResultCode.SUCCESS, zillowService.selectMedianRentByZip(zipcode, time));
    }

    @ApiOperation(value = "租金回报", notes = "租金回报", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "time", value = "时间", required = true, paramType = "query")
    })
    @GetMapping(value = "/rentalyield/{zipcode}")
    public Object rentalYield(@PathVariable("zipcode") String zipcode, @RequestParam(value = "time") String time) throws CustomException {
        return new Result(ResultCode.SUCCESS, zillowService.selectRentalYieldByZip(zipcode, time));
    }

    @ApiOperation(value = "周边房源", notes = "周边房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/circum/house/{houseId}")
    public Object similarHousesNearby(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectSimilarHousesNearby(houseId));
    }

    @ApiOperation(value = "周边数据", notes = "周边数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/periphery/{houseId}")
    public Object periphery(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, homeJunctionService.periphery(houseId));
    }

    @ApiOperation(value = "周边数据-学校", notes = "周边数据-学校", response = School.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/school/{zip}")
    public Object school(@PathVariable String zip) {
        return new Result(ResultCode.SUCCESS, homeJunctionService.school(zip));
    }

    @ApiOperation(value = "周边数据-学校详情", notes = "周边数据-学校详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "id", value = "学校ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/school/detail/{id}")
    public Object schoolDetail(@PathVariable String id) throws CustomException {
        return new Result(ResultCode.SUCCESS, homeJunctionService.schoolDetail(id));
    }

    @ApiOperation(value = "判断房源报告是否已经生成过", notes = "判断房源报告是否已经生成过", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "type", value = "报告类型", required = true, paramType = "query")
    })
    @GetMapping(value = "/exist")
    public Object reportNotExist(String houseId, Integer type) throws CustomException {
        return new Result(ResultCode.SUCCESS, reportService.selectExist(type, houseId));
    }

    @ApiOperation(value = "生成房源报告", notes = "生成房源报告", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "type", value = "报告类型", required = true, paramType = "query")
    })
    @PostMapping(value = "/create/pdf")
    public Object createPDF(String houseId, Integer type) throws CustomException {
        return new Result(ResultCode.SUCCESS, reportService.generate(type, houseId));
    }

    @ApiOperation(value = "创建临时房源报告下载地址（30分钟内有效）", notes = "创建临时房源报告下载地址（30分钟内有效）", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "reportId", value = "报告id", required = true, paramType = "path"),
    })
    @GetMapping(value = "/download/{reportId}")
    public Object createTemporaryUrl(@PathVariable Long reportId) throws CustomException {
        return new Result(ResultCode.SUCCESS, reportService.createTemporaryUrl(reportId));
    }

}
